package com.info.game;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.component.game.Component;
import com.component.game.NullAIComponent;
import com.component.game.NullInputComponent;
import com.component.game.NullPhysicsComponent;
import com.engine.game.Event;
import com.engine.game.GameEngine;
import com.engine.game.GameFabric;
import com.interpolation.game.NullInterpolator;
import com.level.game.Configuration;
import com.level.game.GameObjectInfo;
import com.level.game.Level;
import com.level.game.StageInfo;
import com.render.game.RenderType;

public class FloodItConfiguration extends Configuration{

	public static float initWidth = 500.0f;
	public static float initHeight = 500.0f;
	
	
	public static int counter = 0;
	public static boolean isBlocked = false;
	
	//private float counterWidth = (1.0f/20);
	//private float counterHeight = (2.0f/25);
	
	
	private int counterX = 0;
	private int counterY = 0;
	
	private boolean first = true;
	
	public FloodItConfiguration(GameEngine engine) {
		super(engine);
		
	//	if(FloodItConfiguration.initWidth == 500.0f){
	//		FloodItConfiguration.initWidth = Gdx.graphics.getWidth();
	//		FloodItConfiguration.initHeight = Gdx.graphics.getHeight();
	//	}
		
	//	System.out.println(Gdx.graphics.getWidth() + " " + Gdx.graphics.getHeight());
		
	}

	@SuppressWarnings("serial")
	@Override
	protected void createStates() {
		
		this.mapOfStates.put("PLAY", new ArrayList<Level>(){
			{
				this.add(new GameLevel());
			}
		});
	}

	@Override
	public void setFirstState() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException,
			NoSuchMethodException, SecurityException, IllegalArgumentException,
			InvocationTargetException {
		
		this.currentState = "MENU";
		this.currentLevel = 0;
		
		this.engine.changeTopState(new PlayState(this.engine, new NullInterpolator(), this.mapOfStates.get("PLAY").get(0)));
		
	//	this.setSquares();
	}

	public void setSquares(){
		
	//	System.out.println(Gdx.graphics.getWidth() + " " + Gdx.graphics.getHeight());
		
		float squareWidth = (1.0f/12)*Gdx.graphics.getWidth();
		float squareHeight = (1.0f/10)*Gdx.graphics.getHeight();
		
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_1", "set_color;red", Component.Render));
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_1", "set_bounds;" +
				Float.toString(squareWidth*0.0f) + ";" + Float.toString(Gdx.graphics.getHeight()*(7.0f/10)) + ";" +
				Float.toString(squareWidth) + ";" + Float.toString(squareHeight)
				, Component.Render));
		
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_2", "set_color;blue", Component.Render));
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_2", "set_bounds;" +
				Float.toString(squareWidth*1.0f) + ";" + Float.toString(Gdx.graphics.getHeight()*(7.0f/10)) + ";" +
				Float.toString(squareWidth) + ";" + Float.toString(squareHeight)
				, Component.Render));
		
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_3", "set_color;brown", Component.Render));
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_3", "set_bounds;" +
				Float.toString(squareWidth*2.0f) + ";" + Float.toString(Gdx.graphics.getHeight()*(7.0f/10)) + ";" +
				Float.toString(squareWidth) + ";" + Float.toString(squareHeight)
				, Component.Render));
		
		
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_4", "set_color;green", Component.Render));
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_4", "set_bounds;" +
				Float.toString(squareWidth*0.0f) + ";" + Float.toString(Gdx.graphics.getHeight()*(6.0f/10)) + ";" +
				Float.toString(squareWidth) + ";" + Float.toString(squareHeight)
				, Component.Render));
		
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_5", "set_color;purpule", Component.Render));
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_5", "set_bounds;" +
				Float.toString(squareWidth*1.0f) + ";" + Float.toString(Gdx.graphics.getHeight()*(6.0f/10)) + ";" +
				Float.toString(squareWidth) + ";" + Float.toString(squareHeight)
				, Component.Render));
		
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_6", "set_color;yellow", Component.Render));
		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "MENU_SQUARE_6", "set_bounds;" +
				Float.toString(squareWidth*2.0f) + ";" + Float.toString(Gdx.graphics.getHeight()*(6.0f/10)) + ";" +
				Float.toString(squareWidth) + ";" + Float.toString(squareHeight)
				, Component.Render));
		
		for(int i = 0; i < 196; i++){
			 
			 this.engine.pushEvent(Event.createEvent("CONFIGURATION", "SQUARE_"+Integer.toString(i+1), "set_id;" + Integer.toString(i+1), Component.Render));
			
			 
		 }
		
		float squareWidth1 = (3.0f/56)*Gdx.graphics.getWidth();
		float squareHeight1 = (1.0f/14)*Gdx.graphics.getHeight();
		float quarter = Gdx.graphics.getWidth()*(1.0f/4);
		
		int c = 0;
	/*	
		for(int i = 0; i < 14; i++){
			
			if(i == 13){
				this.createRow(0.0f);
			}
			else{
			this.createRow(Gdx.graphics.getHeight() - ((i+1)*squareHeight));
			}
		//	System.out.println(c);
			//System.out.println(Gdx.graphics.getHeight() - ((i+1)*squareHeight));
		}
		
		*/
		
		for(int i = 0; i < 14; i++){
			for(int j = 0; j < 14; j++){
			
				this.engine.pushEvent(Event.createEvent("CONFIGURATION", "SQUARE_"+Integer.toString(c+1), "set_bounds;" + 
						 Float.toString(quarter + (squareWidth1*j)) + ";" + Float.toString(Gdx.graphics.getHeight() - ((i+1)*squareHeight1)) + ";" +
						 Float.toString(squareWidth1) + ";" + Float.toString(squareHeight1)
				 , Component.Render));
			
				c++;
			
			}
		}
	}
	/*
	public void createRow(float y){
		 float squareWidth = (3.0f/56)*Gdx.graphics.getWidth();
		 float squareHeight = (1.0f/14)*Gdx.graphics.getHeight();
		 
		 float quarter = Gdx.graphics.getWidth()*(1.0f/4);
		 
		// System.out.println(quarter);
		 
		 for(int i = 0; i < 14; i++){
			 
			 
			 
			 
			 this.engine.pushEvent(Event.createEvent("CONFIGURATION", "SQUARE_"+Integer.toString(i+1), "set_bounds;" + 
					 Float.toString(quarter + (squareWidth*i)) + ";" + Float.toString(y) + ";" +
					 Float.toString(squareWidth) + ";" + Float.toString(squareHeight)
			 , Component.Render));
			 
			 
		//	 System.out.println("x:" + (quarter + (squareWidth*i)) + " " + "y:" + y + " " + "width:" + squareWidth + " " + "height:" + squareHeight);
		 }
	}
	*/
	@Override
	public void update() throws InstantiationException, IllegalAccessException,
			ClassNotFoundException, NoSuchMethodException, SecurityException,
			IllegalArgumentException, InvocationTargetException {
		
		if(this.first){
			this.first = false;
			this.setSquares();
		}
		
		
		
//		System.out.println(this.counterWidth + " " + this.counterHeight);
		
//		this.counterWidth *= FloodItConfiguration.initWidth;
//		this.counterHeight *= FloodItConfiguration.initHeight;
		
	//	if(FloodItConfiguration.isBlocked){
	//		this.engine.pushEvent(Event.createEvent("CONFIGURATION", "LABEL",
	//				"end", Component.AI));
	//	}
		
		
		for(Event e : this.events){
			if(e.getMessage().contains("create;COUNTER")){
				
		if(FloodItConfiguration.counter != 25){
		
				
				FloodItConfiguration.counter++;
				
				float counterWidth = (1.0f/20)*Gdx.graphics.getWidth();
				float counterHeight = (2.0f/25)*Gdx.graphics.getHeight();

				
				this.engine.getTopState().addGameObject(GameFabric.createGameObject(this.engine.getTopState(), 
							new GameObjectInfo("COUNTER_" + (FloodItConfiguration.counter), NullInputComponent.class.getCanonicalName(), NullAIComponent.class.getCanonicalName(),
									NullPhysicsComponent.class.getCanonicalName(), SquareRenderComponent.class.getCanonicalName(), false, 
									StageInfo.id, RenderType.gui, -1)));
				
				
				this.engine.pushEvent(Event.createEvent("CONFIGURATION", "COUNTER_" + Integer.toString(FloodItConfiguration.counter),
						"set_color;counter", Component.Render));
				
				this.engine.pushEvent(Event.createEvent("CONFIGURATION", "COUNTER_" + Integer.toString(FloodItConfiguration.counter),
						"set_bounds;" + Float.toString(this.counterX*counterWidth) 
						+ ";" + Float.toString(this.counterY*counterHeight) + ";" +
						Float.toString(counterWidth) + ";" + Float.toString(counterHeight), Component.Render));
				
				
			//	System.out.println("x:" + (this.counterX*this.counterWidth) + " " + "y:" + (this.counterY*this.counterHeight) + 
			//			" " + "width:" + this.counterWidth + " " + "height:" + this.counterHeight);
				
				
				
				if(FloodItConfiguration.counter == 25){
					FloodItConfiguration.isBlocked = true;
				}
			//	System.out.println(FloodItConfiguration.counter + " " + FloodItConfiguration.isBlocked);
	/*			this.counterX = this.counterWidth * (FloodItConfiguration.counter % 5);
				
				if(FloodItConfiguration.counter > 4 && FloodItConfiguration.counter < 10){
					this.counterY = this.counterHeight * 1.0f;
				}
				else if(FloodItConfiguration.counter > 9 && FloodItConfiguration.counter < 15){
					this.counterY = this.counterHeight * 2.0f;
				}
				else if(FloodItConfiguration.counter > 14 && FloodItConfiguration.counter < 20){
					this.counterY = this.counterHeight * 3.0f;
				}
				else if(FloodItConfiguration.counter > 19 && FloodItConfiguration.counter < 25){
					this.counterY = this.counterHeight * 4.0f;
				}
			*/
				
				this.counterX++;
				
				if(this.counterX >= 5){
					this.counterY++;
					this.counterX = 0;
				}
				
		}
				
			}
			else if(e.getMessage().contains("key") && e.getMessage().contains("went_down")){
				
				String[] parts = e.getMessage().split(";");
				
				if(parts[1].equals("Escape") || parts[1].equals("Back")){
					Gdx.app.exit();
				}
				
				
				
			}
			else if(e.getMessage().contains("new_game")){
				FloodItConfiguration.counter = 0;
				this.counterX = 0;
				this.counterY = 0;
				this.first = true;
				FloodItConfiguration.isBlocked = false;
				
				this.engine.getTopState().changeLevel(this.mapOfStates.get("PLAY").get(0));
			}
		}
		
		this.clearEvents();
		
	}

}
